package com.sugon.modules.sparepart.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import com.sugon.dao.SparePartDao;
import com.sugon.dao.SpareStorePosDao;
import com.sugon.entity.*;
import com.sugon.modules.sparepart.consts.MaterialBaseConst;
import com.sugon.modules.sparepart.service.ISparepartStoreManager;
import com.sugon.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ： YuXD
 * @description： 备件库存业务Manager实现类
 * @date ： 2020-09-01 下午 4:14
 * @version: 1.0
 */
@Service
public class SparepartStoreManagerImpl implements ISparepartStoreManager {

    @Autowired
    private SpareStorePosService spareStorePosService;
    @Autowired
    private SpareStorePosExchangeService spareStorePosExchangeService;
    @Autowired
    private SparePartOutService sparePartOutService;
    @Autowired
    private SparePartDao sparePartDao;
    @Autowired
    private SpareStorePosDao spareStorePosDao;
    @Resource
    private DevRepairRecordService devRepairRecordService;
    @Autowired
    private ScarpListService scarpListService;
    @Resource
    private SparePartTransactionRecordService sparePartTransactionService;

    /**
     * exchangeStorePos
     *
     * @param currentStorePos 当前库位
     * @param targetStorePos  目标库位
     * @param exchangeNum     转移数
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean exchangeStorePos(SpareStorePosEntity currentStorePos, SpareStorePosEntity targetStorePos, int exchangeNum) {
        currentStorePos.setStoreNum(currentStorePos.getStoreNum() - exchangeNum);
        targetStorePos.setStoreNum(targetStorePos.getStoreNum() + exchangeNum);
        if (spareStorePosService.update(currentStorePos) > 0 && spareStorePosService.update(targetStorePos) > 0) {
            SpareStorePosExchangeEntity spareStorePosExchangeRecord = new SpareStorePosExchangeEntity();
            spareStorePosExchangeRecord.setOriginalPosId(currentStorePos.getId());
            spareStorePosExchangeRecord.setOriginalSpareCode(currentStorePos.getSpareCode());
            spareStorePosExchangeRecord.setTargetPosId(targetStorePos.getId());
            spareStorePosExchangeRecord.setTargetSpareCode(targetStorePos.getSpareCode());
            spareStorePosExchangeRecord.setExchangeNum(exchangeNum);
            spareStorePosExchangeService.save(spareStorePosExchangeRecord);
        }
        return true;
    }

    /**
     * stockOut
     *
     * @param material      备件
     * @param materialItems 备件清单列表
     * @return
     */
    @Override
    public boolean stockOut(MaterialBaseEntity material, List<MaterialRecordEntity> materialItems) {
        // 备件领料清单出库
        for (MaterialRecordEntity materialItem : materialItems) {
            int num = materialItem.getNum();
            String spareCode = materialItem.getSpareCode();
            String posId = materialItem.getPostionId();
            //获取仓库位置库存信息
            SpareStorePosEntity pos = spareStorePosDao.queryObject(posId);
            // 获取备件信息
            SparePartEntity spare = sparePartDao.queryByCode(spareCode);
            //增加出库记录
            SparePartOutEntity out = new SparePartOutEntity();
            out.setOutDate(DateUtil.date());
            out.setUseUnit(material.getDeptId());
            out.setOutStoreId(pos.getStoreId());
            out.setOutStore(pos.getStoreName());
            out.setLineCode(material.getLineCode());
            out.setPartCode(spareCode);
            out.setName(spare.getName());
            out.setSpec(spare.getSpec());
            out.setPartUnit(spare.getUnit());
            out.setPostionId(pos.getId());
            out.setCargoLocation(pos.getPostion());
            out.setOutNum(num);
            out.setRelNum(num);
            out.setPickNo(material.getMaterCode());
            out.setPickNoId(material.getId());
            // 根据是线边库还是维修领料走不同逻辑
            int materialType = material.getMaterialType();
            if (materialType == MaterialBaseConst.REPAIR_MATERIAL) {
                DevRepairRecordEntity repairRecord = devRepairRecordService.queryObjectByRepairCode(material.getJobCode());
                Assert.notNull(repairRecord);
                out.setRepairNoId(repairRecord.getId());
                out.setRepairNo(repairRecord.getRepairCode());
                out.setUseDevId(repairRecord.getDevId());
                out.setUseDevCode(repairRecord.getDevCode());
                out.setUseDevName(repairRecord.getDevName());
                out.setLineCode(repairRecord.getDevProductCode());
            } else if (materialType == MaterialBaseConst.COMMON_MATERIAL) {
                out.setLineCode(material.getLineCode());
            }
            out.setCreateUser(material.getPicker());
            int rows = sparePartOutService.save(out);
            if (rows > 0) {
                /*
                    由于系统特殊性，备件出库时暂时不更新库位库存、锁定库存和备件实时库存数
                 */
                // 2、实时更新备件库位的库存和锁定库存数
                // spareStorePosService.realtimeUpdateStorePosStock(posId);
                // 3、实时更新备件库存数
                // sparePartDao.realtimeUpdateStock(spareCode);
                // 4、生成备件出库事务记录
                sparePartTransactionService.generateOutTransaction(out);
            }
        }
        return true;
    }

    @Override
    public boolean consumeStockOut(SparePartConsumRecordEntity consumeRecord) {
        SparePartEntity spareParts = sparePartDao.queryObject(consumeRecord.getSparePartId());
        Assert.notNull(spareParts);
        // 1、实时更新备件库位的库存和锁定库存数
        spareStorePosService.realtimeUpdateStorePosStock(consumeRecord.getPosId());
        // 2、实时更新备件库存数
        sparePartDao.realtimeUpdateStock(spareParts.getPartCode());
        // 3、生成备件出库事务记录
        sparePartTransactionService.generateConsumeTransaction(consumeRecord);
        return true;
    }
}
